package chapter_18.demo_2;

/**
 * Created by Administrator on 2018/8/7.
 */
public class CalculableLambda {

    public static Calculable calculable(int power) {

        Calculable calculable;

        if (power == 2) {
            calculable = (int a) -> {
                return a * a;
            };
        } else if (power == 3) {
            calculable = (int a) -> {
                return a * a * a;
            };
        } else {
            calculable = (int a) -> {
                return new Double(Math.pow(a, power)).intValue();
            };
        }

        return calculable;
    }

    public static void main(String[] args) {

        int n1 = 9;
        int n2 = 5;

        // 实现平方计算 Calculable 对象
        Calculable calculable1 = calculable(2);
        System.out.printf("%d^2 = %d \n", n1, calculable1.calculateInt(n1));
        // 实现立方计算 Calculable 对象
        Calculable calculable2 = calculable(3);
        System.out.printf("%d^3 = %d \n", n1, calculable2.calculateInt(n1));
        // 实现立方计算 Calculable 对象
        Calculable calculable3 = calculable(n2);
        System.out.printf("%d^%d = %d \n", n1, n2, calculable3.calculateInt(n1));

    }
}
